﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Config;

public partial class Users_ScoreExchangePoint : BasePage
{
    private int UserId = 0;
    protected int Point = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/ScoreExchangePoint.aspx", "请您登录", true);
        UserId = Base_GetUserInfo.UserId;
        Point = (int)Base_GetUserInfo.Integral;

        if (!IsPostBack)
        {

        }
    }


    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (ExchangeScore())
        {
            ResultStr("ScoreExchangePoint.aspx", "兑换成功!");
        }
        else
        {
            ResultStr("ScoreExchangePoint.aspx", "兑换失败!");
        }
    }

    public bool ExchangeScore()
    {
        int score = StringHelper.isNumber_int(txtScore.Text);
        int point = score / (int)configInfo.PointChangeIntegral;

        if (score >= (int)Base_GetUserInfo.Integral)
        {
            return false;
        }
        else
        {

            //Integral_Model Md = new Integral_Model();
            //Md.AddTime = DateTime.Now.ToString();
            //Md.NetIp = StringHelper.Ip();
            //Md.Num = point;
            //Md.Readme = "用户积分兑换点数(1:" + configInfo.PointChangeIntegral.ToString() + "),扣除积分:" + score + "分,增加" + point + "点";
            //Md.Types = "del";
            //Md.UserId = UserId;
            string IntegralReadme = "用户积分兑换点数(1:" + configInfo.PointChangeIntegral.ToString() + "),扣除积分:" + score + "分,增加" + point + "点";
            string Sqlstr = " BEGIN TRANSACTION ";
            Sqlstr += "update users set integral=integral-{0},userpoint=userpoint+{1} where userId={2}";
            Sqlstr += " Insert into Integral_Log(Num,UserId,Readme,NetIP,Types) values(" + score + "," + UserId + ",'" + IntegralReadme + "','" + StringHelper.Ip() + "','del')  ";
            Sqlstr += " COMMIT TRANSACTION  ";

            Sqlstr = string.Format(Sqlstr, score, point, this.UserId);
            return ExcuteSqlTextBLL.ExcuteResult(Sqlstr) > 0 ? true : false;
        }
    }


}
